﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;
namespace Billing.Transactions
{
    public partial class ServiceChargeTransaction : Billing.Transactions.TransactionBase
    {
        public ServiceChargeTransaction()
        {
            InitializeComponent();
        }
        AccountsView Client = null;
        private void trvClient_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.AccountsSelectionForm frm = new Billing.Win.AccountsSelectionForm(false);
            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                Client = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                this.trvClient.BaseObject = Client;
                this.RefreshAmount();
            }

        }





        public override void OnSave()
        {
            try
            {
                decimal Amount;
                Amount = Convert.ToDecimal(this.txtSrcAmount.Value);
                if (Amount == 0)
                {
                    this.ShowWarning("ნულოვან თანხაზე გატარება დაუშვებელია!");
                    return;
                }


                this.CurrentHeader.HeaderTypeID = 6;
                this.CurrentHeader.Description =this.txtDescription.Text;
               


                this.AddNewLine();
                this.CurrentLine.AccountID = Client.ID;
                this.CurrentLine.ItemID =(short)this.cmbItem.Value;
                this.CurrentLine.TaxID = Client.Client.TaxID;
                this.CurrentLine.Amount = -Amount;
                
                base.OnSave();
            }
            catch (Exception ex)
            {
                this.ShowError(ex);
            }
            this.Close();
        }

        private void ServiceChargeTransaction_Load(object sender, EventArgs e)
        {
            this.bsItem.DataSource = from i in this.BillingDataContext.Items
                                     where i.ItemGroupID == 2
                                     select i;
            this.cmbItem.Value = 7;
        }


        private void RefreshAmount()
        {
            if (this.Client != null)
                this.txtSrcAmount.Value = this.BillingDataContext.GetItemPrice(Client.Client.TaxID, (short)this.cmbItem.Value, this.Client.Client.ClientTypeID);
        }

        private void cmbItem_ValueChanged(object sender, EventArgs e)
        {
            this.RefreshAmount();
        }

    }
}
